# md5   摘要逻辑 hash  计算一致性
# 单向哈希算法，无法根据MD5值反推出原始数据。
# 但是通过撞库 一些简单的是可以被查询出来的
# 网站可能会魔改加盐
import binascii
from hashlib import md5

"""
obj = md5()
obj.update('蔡徐坤'.encode('utf-8')) # 必须先编码 需要是字节
print(obj.hexdigest()) # 32位Hex 17c0fee819b9c79696a382f9634dbd87
obj.update('你干嘛'.encode('utf-8'))
print(obj.hexdigest()) # 86444c15dd8a3f0003948296bc55c7c5 ==> 是6个字一起算的
# obj不能重复使用 它会追加

"""

from hashlib import sha1, sha256, sha512
# obj = sha1()
# obj.update(' 蔡徐坤'.encode('utf-8'))
# print(obj.hexdigest())

from urllib.parse import urlencode, quote, unquote, urljoin
# requests 会自动进行url编码
# 处理cookie特殊字符的时候才有可能会用到
# s = "蔡徐坤,&你干嘛呀"
# print(quote(s)) # 编码
# print(unquote(quote(s))) # 解码

# base_url = "https://www.baidu.com/a/b"
# u1 = "c/d/index.html"
# u2 = "/c/d/index.html"
# print(urljoin(base_url,u1)) # https://www.baidu.com/a/c/d/index.html
# print(urljoin(base_url,u2)) # https://www.baidu.com/c/d/index.html

import base64
# 处理字节的
# bs = "哎哟你干嘛".encode('utf-8')
# print(base64.b64encode(bs).decode()) # 网站会这样处理成字符串
# print(base64.b64decode('5ZOO5ZOf5L2g5bmy5Zib').decode())

# 对称加密 AES DES
# key长度对应关系  16->AES-128  24->AES-192   32->AES-256
# mode(ECB不需要iv CBC需要16位iv) =>这两个比较常用
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad  # 填充

# aes = AES.new(b'c1x2k3n4g5m6a7y8',AES.MODE_ECB)
# s = "蔡徐坤鸡你太美"
# # Data must be aligned to block boundary in ECB mode
# bs = s.encode('utf-8')
# bs = pad(bs,16)
# res = aes.encrypt(bs)
# print(res)
# print(base64.b64encode(res).decode()) # 网页端看到的加密数据就长这样

# 进行解密
# aes = AES.new(b'c1x2k3n4g5m6a7y8',AES.MODE_ECB)
# s = "WvD/XvzBwTRePs6wsbtrrdejNxL0POR5QzXD/296wMw="
# s = base64.b64decode(s)
# res = aes.decrypt(s)
# res = unpad(res,16)
# print(res.decode('utf-8'))

# import binascii
# binascii.a2b_hex("十六进制数字") # 常见于m3u8文件中的iv值 是Hex 需要转换成字节才能使用
# binascii.b2a_hex()


# RSA  非对称加密

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
from Crypto import Random

# key = RSA.generate(2048)
# print(key)
# print(key.export_key().decode())
#
# with open('private.pem','wb') as f:
#     f.write(key.export_key())
#
# p_key = key.public_key().export_key()
# print(p_key.decode())
#
# with open('public.pem','wb') as f:
#     f.write(key.public_key().export_key())



# 进行加密 加载公钥
# p_key = RSA.import_key(open('public.pem','rb').read())
# rsa = PKCS1_v1_5.new(p_key)
# s = "蔡徐坤你干嘛".encode('utf-8')
# bs = rsa.encrypt(s)
# print(bs)
# print(base64.b64encode(bs).decode())

s = "Sqk+n5+P7jDiMAJoZsw18KBBwEivp6HPK4k0fjSve8l5I4lcOlFyiBIFrHkZcleTaxjJA5Zjm5sQHpA4RT5mbxcnaYc6YPhGxfr/jShZ4dV5JmNUO94g85QRqvq2twfUs2aiGxpCC7G6bpKqp5oKjhSOZ0cyjkC6URBSbyw5A5RMBWFIEc7B2bD+MJW46GLpNJ0gZyEq64g8OAF/b4hyiYIaWfnsC6U0NlbY/4yPKGZnROJw7H1q/jxoZNwCoRO4wQxZrwmkbExuSbbf6GOnkt8vQ0j4f/RA3H+rkE0tv535vriWhJLTs9EeOHDeNIvyxwS7YIB0ctYNLN2LxUu7ag=="

key = RSA.import_key(open('private.pem','rb').read())
rsa = PKCS1_v1_5.new(key)
s = base64.b64decode(s)
res = rsa.decrypt(s,"哎哟你干嘛")
print(res.decode("utf-8"))

















